﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;

namespace Maticsoft.Web.sysMan
{
    public partial class ListPaper : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string stuType = this.ddlStuType.SelectedValue;

            string strErr = "";
            if (!PageValidate.IsNumber(txtsCount.Text))
            {
                strErr += "单选题数量格式错误！\\n";
            }
            if (!PageValidate.IsNumber(txtmCount.Text))
            {
                strErr += "多选题数量格式错误！\\n";
            }
            if (!PageValidate.IsNumber(txtjCount.Text))
            {
                strErr += "判断题数量格式错误！\\n";
            }
            if (!PageValidate.IsNumber(txtaCount.Text))
            {
                strErr += "简答题数量格式错误！\\n";
            }
            if (!PageValidate.IsDecimal(txtsScore.Text))
            {
                strErr += "单选题得分格式错误！\\n";
            }
            if (!PageValidate.IsDecimal(txtmScore.Text))
            {
                strErr += "多选题得分格式错误！\\n";
            }
            if (!PageValidate.IsDecimal(txtjScore.Text))
            {
                strErr += "判断题得分格式错误！\\n";
            }
            if (!PageValidate.IsDecimal(txtaScore.Text))
            {
                strErr += "简答题得分格式错误！\\n";
            }
            if (stuType != "模拟")
            {
                if (this.txtpaperTime.Text.Trim().Length == 0)
                {
                    strErr += "考试时长不能为空！\\n";
                }
            }
            else
            {
                if (this.txt_fz.Text.Trim().Length == 0)
                {
                    strErr += "考试时长不能为空！\\n";
                }
            }
            if (!PageValidate.IsNumber(this.txt_jige.Text))
            {
                strErr += "通过分格式错误！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            int sCount = int.Parse(this.txtsCount.Text);
            int mCount = int.Parse(this.txtmCount.Text);
            int jCount = int.Parse(this.txtjCount.Text);
            int aCount = int.Parse(this.txtaCount.Text);
            decimal sScore = decimal.Parse(this.txtsScore.Text);
            decimal mScore = decimal.Parse(this.txtmScore.Text);
            decimal jScore = decimal.Parse(this.txtjScore.Text);
            decimal aScore = decimal.Parse(this.txtaScore.Text);
            int jige = int.Parse(this.txt_jige.Text);
            //string paperTime = this.txtpaperTime.Text;
         
            Maticsoft.Model.papersInfo model = new Maticsoft.Model.papersInfo();
            model.sCount = sCount;
            model.mCount = mCount;
            model.jCount = jCount;
            model.sScore = sScore;
            model.mScore = mScore;
            model.jScore = jScore;
            model.aCount = aCount;
            model.aScore = aScore;
           // model.paperTime = paperTime;
            model.stuType = stuType;
            model.jige = jige;
            

            Maticsoft.BLL.papersInfo bll = new Maticsoft.BLL.papersInfo();
          
            List<Maticsoft.Model.papersInfo> pl = bll.GetModelList(" stuType='"+stuType+"' ");

            if (pl.Count >0)
            {
                model.papersInfoId = pl[0].papersInfoId;

                if (stuType == "模拟")
                {
                    model.paperTime = this.txt_fz.Text;

                    List<Maticsoft.Model.papersInfo> pld = bll.GetModelList(" stuType='党政机关' ");

                    if (pld.Count > 0)
                    {
                        model.sScore = pld[0].sScore;
                        model.mScore = pld[0].mScore;
                        model.jScore = pld[0].jScore;
                        model.aScore = pld[0].aScore;
                    }
                }
                else
                {
                    model.paperTime = this.txtpaperTime.Text;

                    List<Maticsoft.Model.papersInfo> pld = bll.GetModelList(" stuType='模拟' ");

                    if (pld.Count > 0)
                    {
                        pld[0].sScore = model.sScore;
                        pld[0].mScore = model.mScore;
                        pld[0].jScore = model.jScore;
                        pld[0].aScore = model.aScore;
                        bll.Update(pld[0]);
                    }
                }

                if (bll.Update(model))
                {
                    bll.UpdateScore("singleInfo", " stuType='" + model.stuType + "' ", "sscore", model.sScore.Value);
                    bll.UpdateScore("mostInfo", " stuType='" + model.stuType + "' ", "mscore", model.mScore.Value);
                    bll.UpdateScore("judgeInfo", " stuType='" + model.stuType + "' ", "jscore", model.jScore.Value);
                    bll.UpdateScore("answerinfo", " stuType='" + model.stuType + "' ", "ascore", model.aScore.Value);
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "ListPaper.aspx");
                }
            }
            else
            {
                if (stuType == "模拟")
                {
                    model.paperTime = this.txt_fz.Text;

                    List<Maticsoft.Model.papersInfo> pld = bll.GetModelList(" stuType='党政机关' ");

                    if (pld.Count > 0)
                    {
                        model.sScore = pld[0].sScore;
                        model.mScore = pld[0].mScore;
                        model.jScore = pld[0].jScore;
                        model.aScore = pld[0].aScore;
                    }
                }
                else
                {
                    model.paperTime = this.txtpaperTime.Text;

                    List<Maticsoft.Model.papersInfo> pld = bll.GetModelList(" stuType='模拟' ");

                    if (pld.Count > 0)
                    {
                        pld[0].sScore = model.sScore;
                        pld[0].mScore = model.mScore;
                        pld[0].jScore = model.jScore;
                        pld[0].aScore = model.aScore;
                        bll.Update(pld[0]);
                    }
                }

                if(bll.Add(model) > 0)
                {
                    bll.UpdateScore("singleInfo", " stuType='" + model.stuType + "' ", "sscore", model.sScore.Value);
                    bll.UpdateScore("mostInfo", " stuType='" + model.stuType + "' ", "mscore", model.sScore.Value);
                    bll.UpdateScore("judgeInfo", " stuType='" + model.stuType + "' ", "jscore", model.sScore.Value);
                    bll.UpdateScore("answerinfo", " stuType='" + model.stuType + "' ", "ascore", model.aScore.Value);
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "ListPaper.aspx");
                }

                    
            }
            
            
        }


        private void Bind()
        {
            string stuType = this.ddlStuType.SelectedValue;

            List<Maticsoft.Model.papersInfo> pl = new Maticsoft.BLL.papersInfo().GetModelList(" stuType = '"+stuType+"' ");

            if (pl.Count > 0)
            {
                Maticsoft.Model.papersInfo model = pl[0];
                this.txtsCount.Text = model.sCount.ToString();
                this.txtmCount.Text = model.mCount.ToString();
                this.txtjCount.Text = model.jCount.ToString();
                this.txtsScore.Text = model.sScore.ToString();
                this.txtmScore.Text = model.mScore.ToString();
                this.txtjScore.Text = model.jScore.ToString();
                this.txt_jige.Text = model.jige.ToString();
                this.txtaCount.Text = model.aCount.ToString();
                this.txtaScore.Text = model.aScore.ToString();
                if (model.stuType == "模拟")
                {
                    this.txtjScore.Enabled = false;
                    this.txtmScore.Enabled = false;
                    this.txtsScore.Enabled = false;
                    this.txtaScore.Enabled = false;
                    List<Maticsoft.Model.papersInfo> pld = new Maticsoft.BLL.papersInfo().GetModelList(" stuType = '党政机关' ");

                    if (pld.Count > 0)
                    {
                        this.txtsScore.Text = pld[0].sScore.ToString();
                        this.txtmScore.Text = pld[0].mScore.ToString();
                        this.txtjScore.Text = pld[0].jScore.ToString();
                        this.txtaScore.Text = pld[0].aScore.ToString();
                    }
                    else
                    {
                        this.txtsScore.Text = "0";
                        this.txtmScore.Text = "0";
                        this.txtjScore.Text = "0";
                        this.txtaScore.Text = "0";
                    }

                    this.txt_fz.Visible = true;
                    this.lbl_fz.Visible = true;
                    this.txtpaperTime.Visible = false;

                    this.txt_fz.Text = model.paperTime;
                }
                else
                {
                    this.txtjScore.Enabled = true;
                    this.txtmScore.Enabled = true;
                    this.txtsScore.Enabled = true;
                    this.txtaScore.Enabled = true;
                    this.txt_fz.Visible = false;
                    this.lbl_fz.Visible = false;
                    this.txtpaperTime.Visible = true;

                    this.txtpaperTime.Text = model.paperTime;

                }
               
            }
            else
            {
                if (this.ddlStuType.SelectedValue == "模拟")
                {
                    this.txtsCount.Text = "";
                    this.txtmCount.Text = "";
                    this.txtjCount.Text = "";
                    this.txtaCount.Text = "";
                    this.txt_jige.Text = "";

                    List<Maticsoft.Model.papersInfo> pld = new Maticsoft.BLL.papersInfo().GetModelList(" stuType = '党政机关' ");

                    if (pld.Count > 0)
                    {
                        this.txtsScore.Text = pld[0].sScore.ToString();
                        this.txtmScore.Text = pld[0].mScore.ToString();
                        this.txtjScore.Text = pld[0].jScore.ToString();
                        this.txtaScore.Text = pld[0].aScore.ToString();
                    }
                    else
                    {
                        this.txtsScore.Text = "0";
                        this.txtmScore.Text = "0";
                        this.txtjScore.Text = "0";
                        this.txtaScore.Text = "0";
                    }

                    this.txtjScore.Enabled = false;
                    this.txtmScore.Enabled = false;
                    this.txtsScore.Enabled = false;
                    this.txtaScore.Enabled = false;
                    this.txt_fz.Visible = true;
                    this.lbl_fz.Visible = true;
                    this.txtpaperTime.Visible = false;
                }
                else
                {
                    this.txtsCount.Text = "";
                    this.txtmCount.Text = "";
                    this.txtjCount.Text = "";
                    this.txtsScore.Text = "";
                    this.txtmScore.Text = "";
                    this.txtjScore.Text = "";
                    this.txt_jige.Text = "";
                    this.txtaCount.Text = "";
                    this.txtaScore.Text = "";
                    this.txtjScore.Enabled = true;
                    this.txtmScore.Enabled = true;
                    this.txtsScore.Enabled = true;
                    this.txtaScore.Enabled = true;
                    this.txt_fz.Visible = false;
                    this.lbl_fz.Visible = false;
                    this.txtpaperTime.Visible = true;


                }
              
            }
        }

        protected void ddlStuType_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind();

        }
    }
}